package com.zmn.oms.task.listener;


import com.alibaba.fastjson.JSONObject;
import com.zmn.consts.GlobalConsts;
import com.zmn.manager.mq.annotation.AliyunMQHandler;
import com.zmn.manager.mq.annotation.AliyunMQListener;
import com.zmn.oms.business.interfaces.masterphoto.OrderPhotoBService;
import com.zmn.oms.common.constant.MessageQueueTagConsts;
import com.zmn.oms.model.dto.order.photo.OrderPhotoDetectionDTO;
import com.zmn.oms.model.dto.order.photo.OrderPhotoDetectionResultDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 类描述：工单照片 - 自动审核监听器
 *
 * @author sunlife
 * @date:2021/11/10 5:23 下午
 * description:
 */
@AliyunMQListener(topic = "${spring.mq.topic.oms}")
@Component
@Slf4j
public class OrderImageDetectionListener {
    @Autowired
    OrderPhotoBService orderPhotoBService;

    /**
     * 自动审核照片
     *
     * @param message
     */
    @AliyunMQHandler(tags = MessageQueueTagConsts.ORDER_PHOTO_DETECTION_TAG)
    public void photoDetection(String message) {
        log.info("自动发送审核照片请求，入参：{}", message);
        try {
            // 处理数据
            OrderPhotoDetectionDTO dto = JSONObject.parseObject(message, OrderPhotoDetectionDTO.class);

            dto.setOperator("系统");
            dto.setOperatorId((long)GlobalConsts.OPERATE_USER_TYPE_SYSTEM);
            dto.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_SYSTEM);

            orderPhotoBService.sendImageDetectionRequest(dto);
        } catch (Exception e) {
            log.error("发送照片自动审核请求失败，原因：{}", e);
        }

    }

    /**
     * 得到 【照片的审核结果】 之后的回调。
     *
     * @author liudi
     * @since: 2022/2/7 14:10
     * @param message 照片的审核结果
     */
    @AliyunMQHandler(tags = MessageQueueTagConsts.ORDER_PHOTO_DETECTION_CALLBACK_TAG)
    public void photoDetectionCallback(String message) {
        log.info("得到【照片审核结果】回调 入参：{}", message);
        try {
            // 处理数据
            OrderPhotoDetectionResultDTO dto = JSONObject.parseObject(message, OrderPhotoDetectionResultDTO.class);
            dto.setOperator("系统");
            dto.setOperatorId((long)GlobalConsts.OPERATE_USER_TYPE_SYSTEM);
            dto.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_SYSTEM);

            // 开始自动审核
            orderPhotoBService.autoAuditOrderPhoto(dto);
        } catch (Exception e) {
            log.error("处理【照片审核结果】失败，原因：{}", e);
        }

    }
}
